package com.tapstitch.scm.pms.web.config;

import com.bluebuck.quickboot.core.api.dto.ApiResult;
import com.bluebuck.quickboot.core.exception.BusinessException;
import com.tapstitch.scm.pms.web.facade.AlertFacade;
import com.tapstitch.scm.common.core.domain.JsonResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.security.auth.login.AccountExpiredException;
import java.nio.file.AccessDeniedException;

/**
 * 全局异常处理器
 *
 * @author hornbill
 */
@RestControllerAdvice(annotations = {RestController.class, Controller.class})
public class GlobalExceptionHandler {

    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @Autowired
    private AlertFacade alertFacade;

    /**
     * 业务异常
     */
    @ExceptionHandler(BusinessException.class)
    public ApiResult<?> businessException(BusinessException e) {
        return ApiResult.result(e.getCode(), e.getMessage(), e.getData());
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public ApiResult<?> handlerNoFoundException(Exception e) {
        log.error(e.getMessage(), e);
        return ApiResult.result(JsonResultCode.NOT_FOUND, "路径不存在，请检查路径是否正确", null);
    }

    @ExceptionHandler(AccessDeniedException.class)
    public ApiResult<?> handleAuthorizationException(AccessDeniedException e) {
        log.error(e.getMessage());
        return ApiResult.result(JsonResultCode.FORBIDDEN, "没有权限，请联系管理员授权", null);
    }

    @ExceptionHandler(AccountExpiredException.class)
    public ApiResult<?> handleAccountExpiredException(AccountExpiredException e) {
        log.error(e.getMessage(), e);
        return ApiResult.sysErrorResult(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public ApiResult<?> handleException(Exception e) {
        log.error(e.getMessage(), e);
        alertFacade.sendMsg(e);
        return ApiResult.sysErrorResult("系统异常");
    }

    /**
     * 自定义验证异常
     */
    @ExceptionHandler(BindException.class)
    public ApiResult<?> validatedBindException(BindException e) {
        log.error(e.getMessage(), e);
        String message = e.getAllErrors().get(0).getDefaultMessage();
        return ApiResult.sysErrorResult(message);
    }

    /**
     * 自定义验证异常
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Object validExceptionHandler(MethodArgumentNotValidException e) {
        log.error(e.getMessage(), e);
        String message = e.getBindingResult().getFieldError().getDefaultMessage();
        return ApiResult.sysErrorResult(message);
    }

    /**
     * Assert.notnull ...异常统一处理
     * @param e 非法参数
     * @return
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public Object assertExceptionHandler(IllegalArgumentException e) {
        log.error(e.getMessage(), e);
        return ApiResult.paramErrorResult(e.getMessage());
    }

}
